package com.psg.controller;

import com.psg.exception.RuleDoesNotMeetException;
import com.psg.pojo.Address;
import com.psg.service.AddressService;
import com.psg.util.Result;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;
import java.util.regex.Pattern;

@RestController
@RequestMapping("/user/address")
public class AddressController extends BaseController{

    @Autowired
    private AddressService addressService;

    @RequestMapping("/list")
    private ModelAndView toAddress(){
        ModelAndView mav = new ModelAndView();
        // 查询所有地址
        Integer userId = getUserId();
        if (null==userId) {
            mav.setViewName("login");
            return mav;
        }

        List<Address> addressList = addressService.findAllAddressByUserId(userId);
        mav.addObject(addressList);
        mav.setViewName("address");
        return mav;
    }

    @PostMapping("/save")
    public Result save(@RequestBody Address address){
        // 校验参数
        if (StringUtils.isBlank(address.getName())) return Result.error("收件人不能为空！");
        if (StringUtils.isBlank(address.getDetailAddress())) return Result.error("收件地址不能为空！");
        if(StringUtils.isBlank(address.getPhone())) return Result.error("手机号码不能为空！");
        final String phoneRegex = "^1[3456789]\\d{9}$";
        if (!Pattern.matches(phoneRegex,address.getPhone())) return Result.error("请填写正确的手机号码！");
        final String codeRegex = "^[1-9]\\d{5,6}$";
        if (!Pattern.matches(codeRegex,address.getCode())) return Result.error("请填写正确的邮编！");
        address.setUserId(getUserId());
        try {
            addressService.save(address);
            return Result.success();
        }catch (RuleDoesNotMeetException e){
            return Result.error(e.getMessage());
        }catch (Exception e) {
            e.printStackTrace();
            return Result.error("保存地址信息失败！");
        }
    }

    @PostMapping("/update")
    public Result update(@RequestBody Address address){
        // 校验参数
        if (null == address.getId()) return Result.error("请求参数异常");
        if (StringUtils.isBlank(address.getName())) return Result.error("收件人不能为空！");
        if (StringUtils.isBlank(address.getDetailAddress())) return Result.error("收件地址不能为空！");
        if(StringUtils.isBlank(address.getPhone())) return Result.error("手机号码不能为空！");
        final String phoneRegex = "^1[3456789]\\d{9}$";
        if (!Pattern.matches(phoneRegex,address.getPhone())) return Result.error("请填写正确的手机号码！");
        final String codeRegex = "^[1-9]\\d{5,6}$";
        if (!Pattern.matches(codeRegex,address.getCode())) return Result.error("请填写正确的邮编！");
        address.setUserId(getUserId());
        try {
            addressService.update(address);
            return Result.success();
        }catch (RuleDoesNotMeetException e){
            return Result.error(e.getMessage());
        }catch (Exception e) {
            e.printStackTrace();
            return Result.error("保存地址信息失败！");
        }
    }

    @PostMapping("/changeDefault")
    public Result changeDefault(int id){
        try {
            Integer userId = getUserId();
            addressService.changeDefault(id,userId);
            return Result.success();
        }catch (RuleDoesNotMeetException e){
            e.printStackTrace();
            return Result.error(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("保存地址信息失败！");
        }
    }
}
